package com.lepu.app.fun.chat.util;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.app.application.MyApplication;
import com.eyzhs.app.R;
import com.lepu.app.fun.chat.controller.base.BaseFragmentActivityChat;

import common.db.Constants;


public class CommonPopUpWindow extends PopupWindow {

    private CommonPopUpWindow popwindow;

    private View layout;
    static PopupWindow deletePopWindow;

    public CommonPopUpWindow(Context context, int myLayout) {
        LayoutInflater inflater = LayoutInflater.from(context);
        layout = inflater.inflate(myLayout, null);

        //设置SelectPicPopupWindow的View
        this.setContentView(layout);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        Drawable dw = new ColorDrawable();
        dw.setAlpha(180);
        this.setBackgroundDrawable(dw);
        show();
    }

    public static void setLongclickDeletePopView(Context context, View parentView, final DataHandler listener) {
        View popView = LayoutInflater.from(context).inflate(R.layout.popwindow_del_long_click, null);
        deletePopWindow = new PopupWindow(popView, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        deletePopWindow.getContentView().measure(0, 0);
        int popupWindowHeight = deletePopWindow.getContentView().getMeasuredHeight();
        int popupWindowWidth = deletePopWindow.getContentView().getMeasuredWidth();

        TextView delTagTxt = (TextView) popView.findViewById(R.id.txt_del_tag);
        delTagTxt.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.onData(Constants.DELETE_CLICK, "", "");
                deletePopWindow.dismiss();
            }
        });

        deletePopWindow.setOutsideTouchable(true);
        deletePopWindow.setBackgroundDrawable(new ColorDrawable());
        int[] location = new int[2];
        parentView.getLocationOnScreen(location);
        deletePopWindow.showAtLocation(parentView, Gravity.NO_GRAVITY, parentView.getWidth() / 2 - popupWindowWidth / 2, location[1] + parentView.getHeight() / 2 - popupWindowHeight / 2);
    }

    public static void hideLongclickDeletePopView() {
        if (deletePopWindow != null) {
            deletePopWindow.dismiss();
        }
    }

    private void show() {
        MyApplication context = MyApplication.getInstance();
        BaseFragmentActivityChat currentActivity = context.getCurrentActivity();
        //// TODO: 2015/10/13  
//        && currentActivity.getCurrentFragment() instanceof DoctorDetailFromChatFragment
        if (currentActivity != null && currentActivity.getCurrentFragment() != null
                && currentActivity.getCurrentFragment().getView() != null) {
            View parent = currentActivity.getCurrentFragment().getView();
            this.showAtLocation(parent, Gravity.CENTER, 0, 0);
        } else if (currentActivity != null && currentActivity.getCurrentFragment() != null
                && currentActivity.getCurrentFragment().getView() != null) {
            View parent = currentActivity.getCurrentFragment().getView();
            this.showAtLocation(parent, Gravity.CENTER, 0, 0);
        } else
        //// TODO: 2015/10/13  
//        if (currentActivity != null && currentActivity instanceof XdoctorActivity){
//            View view = currentActivity.getCurrentFocus();
//            if (view != null) {
//                this.showAtLocation(view, Gravity.CENTER, 0, 0);
//            }
//        } else

        {
            //// TODO: 2015/10/13  
//            if (MyApplication.getInstance() instanceof MainActivity) {
//                View parent = AppContext.getAppContext().getMainView();
//                this.showAtLocation(parent, Gravity.CENTER, 0, 0);
//            }
        }

    }

    public View getPoPView() {
        return layout;
    }

    public void registerClick(int id, OnClickListener listener) {
        if (layout.findViewById(id) != null) {
            View clickView = layout.findViewById(id);
            clickView.setOnClickListener(listener);
        }
    }

    public void registerClick(int id, String content, OnClickListener listener) {
        if (layout.findViewById(id) != null) {
            View clickView = layout.findViewById(id);
            if (clickView instanceof Button && content != null) {
                ((Button) clickView).setText(content);
            } else if (clickView instanceof TextView && content != null) {
                ((TextView) clickView).setText(content);
            }

            clickView.setOnClickListener(listener);
        }
    }

    public void setTextContent(int id, String str) {
        if (layout.findViewById(id) != null) {
            View txtView = (View) layout.findViewById(id);
            if (txtView instanceof Button && str != null) {
                ((Button) txtView).setText(str);
            } else if (txtView instanceof TextView && str != null) {
                ((TextView) txtView).setText(str);
            }
        }
    }

    public void registerDismissClick(int id) {
        //临时
        if (id == R.id.layout_bg) {
            return;
        }
        if (layout.findViewById(id) != null) {
            View clickView = layout.findViewById(id);
            clickView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    dismiss();
                }
            });
        }
    }


}

